//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.base.domain.SystemClass;
import com.ruoyi.live.base.service.ILiveExamClassService;
import com.ruoyi.live.base.service.ISystemClassService;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级管理
 */
@RestController
@RequestMapping({"/base/class"})
public class SystemClassController extends BaseController {
    @Autowired
    ISystemClassService systemClassService;

    @Autowired
    ILiveExamClassService liveExamClassService;

    @Autowired
    ISysConfigService sysConfigService;

    @GetMapping("/list")
    public TableDataInfo query(SystemClass entity) {
        this.startPage();
        QueryWrapper<SystemClass> wrapper = new QueryWrapper<>(entity);
        List<SystemClass> list = systemClassService.list(wrapper);
        return this.getDataTable(list);
    }

    /**
     * 根据班级编号获取详细信息
     */
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId)
    {
        return AjaxResult.success(systemClassService.getById(classId));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin,teacher')")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SystemClass entity) {
        QueryWrapper<SystemClass> wrapper = new QueryWrapper<>();
        wrapper.eq("class_name", entity.getClassName());
        SystemClass existEntity = systemClassService.getOne(wrapper);
        if (existEntity == null) {
            entity.setCreateTime(System.currentTimeMillis());
            entity.setCreateUser(SessionInfo.getUserId());
            entity.setState(1);
            return toAjax(systemClassService.save(entity));
        }
        else {
            return error("班级名称:" + entity.getClassName()  + "已存在");
        }
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SystemClass entity) {
        return toAjax(systemClassService.updateById(entity));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classIds}")
    public AjaxResult delete(@PathVariable Long[] classIds) {
        UpdateWrapper<SystemClass> wrapper = new UpdateWrapper<>();
        Integer count = systemClassService.deleteBatchIds(classIds);
        if (count > 0 && count.equals(classIds.length)) {
            return success();
        }
        else {
            return error();
        }
    }
}
